<?php
/**
 * Description: 新品速递用户收货地址
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/9/9
 * Time: 14:04
 */


ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('memory_limit', '1024M');

require_once '/data/wwwroot/g_vendor/autoload.php';


$start = microtime(true);
$page = 1;
$pageSize = 1000;
$lists = [];
$all_num = 0;

do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    $where = [
        "LIMIT" => $limit,
        'area[!]' => ''
    ];

    $lists = \Biz\model\brandActivity\UserAddressModel::all($where, '*');

    $data = [];

    foreach ($lists as $list) {
        $update =[];
        try {
            $codes = explode(",", $list['area']);

            if ($codes) {
                $code = array_pop($codes);
                $code = \Hhz\AdministrativeRegion\Region::getCodeByOldCode($code);
                $update['area_code'] = $code;
                $update['area_name'] = \Hhz\AdministrativeRegion\Region::getAreaDetailByCode($code);
                if ($update) {
                    \Biz\model\brandActivity\UserAddressModel::update($update, ['id' => $list['id']]);
                }
            }
//            if ($codes) {
//                foreach ($codes as $code) {
//                    $codeArea = \Hhz\AdministrativeRegion\Region::getCodeByOldCode($code) ?? '';
//                    if ($code) {
//                        $data[] = $codeArea;
//                    }
//                }
//                $update['area_code'] = implode(',', $data);
//            }
//            if ($update){
//                \Biz\model\brandActivity\UserAddressModel::update($update, ['id' => $list['id']]);
//            }
        } catch (\Exception $e) {
            print $e->getMessage();
            exit();
        }
    }
    $page++;
} while ($lists);

$end = microtime(true);
$time = $end - $start;
$second = round($time, 3);
var_dump('完成时间：' . date('Y-m-d H:i:s') . '   总耗时:' . $second . '秒');

